package com.teama4.shop.controller;

import com.teama4.shop.annotation.NeedLogin;
import com.teama4.shop.domain.ViewHistoryExample;
import com.teama4.shop.mapper.ViewHistoryMapper;
import com.teama4.shop.service.ViewHistoryService;
import com.teama4.shop.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/ViewHistory")
public class ViewHistoryController {

    @Autowired
    private ViewHistoryService viewHistoryService;

    /**
     * 删除浏览记录
     *
     * @param req
     * @param resp
     * @param viewHistoryId
     * @throws UnsupportedEncodingException
     */
    @NeedLogin
    @RequestMapping("/delViewHistory")
    public void delViewHistory(HttpServletRequest req, HttpServletResponse resp, Integer viewHistoryId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        HttpSession session = req.getSession(false);
        if (session != null) {
            int res = viewHistoryService.deleteByPrimaryKey(viewHistoryId);
            if (res > 0) {
                //如果删除成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果删除失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**
     * 删除所选浏览记录
     *
     * @param req
     * @param resp
     * @param allCheckId
     * @throws UnsupportedEncodingException
     */
    @NeedLogin
    @RequestMapping("/delCheckedViewHistory")
    public void delCheckedViewHistory(HttpServletRequest req, HttpServletResponse resp, String allCheckId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        if (allCheckId != null && allCheckId != "") {
            String[] allCheckIds = allCheckId.split(",");

            HttpSession session = req.getSession(false);
            if (session != null) {
                ViewHistoryExample viewHistoryExample = new ViewHistoryExample();
                ViewHistoryExample.Criteria criteria = viewHistoryExample.createCriteria();
                List<Integer> allCheckIdsList = new ArrayList<>();

                for (int i = 0; i < allCheckIds.length; i++) {
                    allCheckIdsList.add(Integer.valueOf(allCheckIds[i]));
                }
                criteria.andIdIn(allCheckIdsList);
                int res = viewHistoryService.deleteByExample(viewHistoryExample);
                if (res > 0) {
                    //如果删除成功
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果删除失败
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);
                }
            } else {
                //如果session获取不到值
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "noSession");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "isNull");
            JSONUtil.printByJSON(resp, map);
        }
    }

}
